/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.asiainfo.utils.sql.sqlbuilder.select;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import javax.sql.DataSource;

import com.asiainfo.utils.sql.sqlbuilder.Database;

public class QueryBuilder {
	private final Context context;

	public QueryBuilder(Database database, DataSource dataSource) throws SQLException {
		this(database, dataSource.getConnection());
	}

	public QueryBuilder(Database database, Connection connection) {
		this.context = new Context(database, connection);
	}

	public Select select() {
		return new Select(this.context);
	}

	public String getSafeSQL() throws SQLException {
		return this.context.getSafeSql();
	}

	public HashMap find() throws SQLException {
		return this.context.single();
	}

	public List<HashMap> findAll() throws SQLException {
		return this.context.list();
	}

	public HashMap find(String sql) throws SQLException {
		return this.context.getRow(this.context.query(sql));
	}

	public List<HashMap> findAll(String sql) throws SQLException {
		return this.context.getRows(this.context.query(sql));
	}

	public boolean execute(String sql) throws SQLException {
		return this.context.execute(sql);
	}

	public <E> E single(RowMapper<E> rowMapper) throws SQLException {
		return this.context.single(rowMapper);
	}

	public <E> List<E> list(RowMapper<E> rowMapper) throws SQLException {
		return this.context.list(rowMapper);
	}

	public void close() {
		this.context.close();
	}

	public List getTableNames() throws SQLException {
		return this.context.getTables();
	}

	public boolean isExistsTable(String tablename) throws SQLException {
		return this.context.getTables().contains(tablename);
	}

	public String toString() {
		return this.context.toString();
	}
}